package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Student;
import com.example.springboot.entity.SysRole;
import com.example.springboot.entity.SysUserRole;
import com.example.springboot.entity.vo.AssignParm;
import com.example.springboot.entity.vo.AssignVo;
import com.example.springboot.entity.vo.SaveRoleParm;
import com.example.springboot.mapper.SysRoleMapper;
import com.example.springboot.service.SysRoleMenuService;
import com.example.springboot.service.SysRoleService;
import com.example.springboot.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/role")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;
    /**
     * 添加
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody SysRole sysRole) {

        int i = sysRoleService.addnew(sysRole);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "添加失败");
        }

    }
    /**
     * 删除学生信息
     */

    @DeleteMapping("/delete/{roleId}")
    public Result<?> delete(@PathVariable Integer roleId) {
        int i = sysRoleService.deleteRole(roleId);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "删除失败");
        }
    }

    /**
     * 分页条件查询  根据角色名称查询
     */
    @GetMapping("/find")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page page = sysRoleService.find(pageNum, pageSize, search);
        if (page != null) {
            return Result.success(page);
        } else {
            return Result.error("-1", "查询失败");
        }
    }
    /**
     * 更新信息
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody  SysRole sysRole) {
        int i = sysRoleService.updateRole(sysRole);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "更新失败");
        }
    }

    /**
     * 分页条件查询  根据角色名称查询
     */
    @GetMapping("/findrole")
    public Result<?> findPagerole(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  @RequestParam(defaultValue = "") String search) {
        Page page = sysRoleService.find(pageNum, pageSize, search);
        if (page != null) {
            return Result.success(page.getRecords());
        } else {
            return Result.error("-1", "查询失败");
        }
    }
    //查询用户对应角色
    //
    @GetMapping("/getRoleByUserId/{userId}")
    public Result<?> getRoleByUserId(@PathVariable Long userId) {
        QueryWrapper<SysUserRole> query=new QueryWrapper<>();
        query.lambda().eq(SysUserRole::getUserId,userId);
        SysUserRole one=sysUserRoleService.getOne(query);
        return  Result.success(one);
    }
    @GetMapping("/getRoleName/{userId}")
    public Result<?> getRoleName(@PathVariable Long userId) {
        QueryWrapper<SysUserRole> query=new QueryWrapper<>();
        query.lambda().eq(SysUserRole::getUserId,userId);
        SysUserRole one=sysUserRoleService.getOne(query);
        if (one.getRoleId()!=null) {
            SysRole name = sysRoleMapper.selectById(one.getRoleId());
            return Result.success(name);
        }else {
            return Result.error("-1", "查询失败");
        }
    }
    //分配权限回显
    @GetMapping("/getAssingShow/{userId}/{roleId}")
    public Result<?> getAssingShow(@PathVariable(required = false) Long userId,
                                   @PathVariable(required = false) Long roleId) {
        AssignParm parm = new AssignParm();
        parm.setUserId(userId);
        parm.setRoleId(roleId);
        AssignVo show = sysRoleService.getAssignShow(parm);
        return Result.success(show);
    }
    @PostMapping("/saveRoleMenu")
    public Result<?> saveRoleMenu(@RequestBody SaveRoleParm parm){
        sysRoleMenuService.saveRoleMenu(parm.getRoleId(),parm.getList());
        return Result.success();
    }
}
